package com.hang.controller;

import com.hang.common.Result;
import com.hang.exception.MyException;
import com.hang.model.dto.base.BaseModel;
import com.hang.model.dto.request.ItemPageDTO;
import com.hang.model.dto.request.NoticeModelDTO;
import com.hang.model.vo.NoticeInfoDetailVO;
import com.hang.model.vo.NoticeInfoVO;
import com.hang.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "公告管理模块")
@RestController
@RequestMapping("notice")
@Slf4j
public class NoticeController {
    @Resource
    private INoticeService noticeService;

    @ApiOperation(value = "发布公告")
    @PostMapping("publish")
    public Result<Object> publishNotice(
            @RequestBody @Valid NoticeModelDTO noticeModelDTO
    ){
        try {
            noticeService.createNotice(noticeModelDTO);
            return Result.ok().message("发布成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "查看公告列表")
    @PostMapping("getNoticeListByPageable")
    public Result<List<NoticeInfoVO>> getNoticeListByPageable(
            @RequestBody @Valid ItemPageDTO itemPageDTO
    ){
        List<NoticeInfoVO> noticeInfoVOList = noticeService.getNoticeListByPage(itemPageDTO);
        return Result.ok(noticeInfoVOList);
    }

    @ApiOperation(value = "删除公告")
    @DeleteMapping("delete")
    public Result<Object> deleteNotice(@RequestParam("noticeId") Long noticeId,@RequestParam("userId") Long userId){
        try {
            noticeService.deleteNotice(noticeId,userId);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "查看公告详情")
    @GetMapping("detail")
    public Result<NoticeInfoDetailVO> getDetailByNoticeId(@RequestParam("noticeId") Long noticeId){
        NoticeInfoDetailVO noticeInfoDetailVO = null;
        try {
             noticeInfoDetailVO = noticeService.getNoticeInfoDetailById(noticeId);
            return Result.ok(noticeInfoDetailVO);
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail(noticeInfoDetailVO).message(me.getMessage());
        }
    }

}
